﻿$(document).ready(function () {
	var type = $("#types"),
    value = $("#values"),
    allFields = $([]).add(type).add(value),
    tips = $(".validateTips");

	function updateTips(t) {
	    tips
		.text(t)
		.addClass("ui-state-highlight");
	    setTimeout(function () {
	        tips.removeClass("ui-state-highlight", 1500);
	    }, 500);
	}

	function checkLength(o, n, min, max) {
	    if (o.val().length > max || o.val().length < min) {
	        o.addClass("ui-state-error");
	        updateTips("Length of " + n + " must be between " + min + " and " + max + ".");
	        return false;
	    } else {
	        return true;
	    }
	}

	function reload() {
	    $("table[Type]").children().remove();
        $.ajax({
            type: 'POST',
            url: '/Project/GetAllLookups?ProjectId=' + $("#ProjectId").val(),
            success: function (data) {
                $.each(data, function (i) {
                    $("table[Type='" + data[i].Type + "']").append("<tr><td>" + data[i].Value + "</td><td><span class='btnDelete' Type='" + data[i].Type + "' Value='" + data[i].Value + "'>Delete</span></td></tr>");
                    $(".btnDelete").click(function () {
                        delType = $(this).attr("Type");
                        delValue = $(this).attr("Value");
                        row = $(this).parent().parent();
                        $("#dialog-confirm").dialog("open");
                    });
                });
            },
            dataType: 'json'
        });
	}

	var delType, delValue, row;
	$("#dialog-confirm").dialog({
	    autoOpen: false,
	    resizable: false,
	    height: 140,
	    modal: true,
	    buttons: {
	        "Delete": function () {
//	                    $.getJSON('/Project/DeleteLookups?ProjectId=' + $("#ProjectId").val() + '&Type=' + delType + '&Value=' + delValue,
//                            function (data) {
//                                if (data.Success == "False") {
//                                    $("#modal-msg").html("<br />" + data.Message);
//                                    $("#dialog-message").dialog("open");
//                                    return;
//                                }
//                                else {
//                                    reload();
//                                    $("#dialog-confirm").dialog("close");
//                                }
	            //                            });
	            $.ajax({
	                type: 'POST',
	                url: '/Project/DeleteLookups?ProjectId=' + $("#ProjectId").val() + '&Type=' + delType + '&Value=' + delValue,
	                success: function (data) {
	                    if (data.Success == "False") {
                            $("#modal-msg").html("<br />" + data.Message);
                            $("#dialog-message").dialog("open");
                            return;
                        }
                        else {
                            reload();
                            $("#dialog-confirm").dialog("close");
                        }
	                },
	                dataType: 'json'
	            });

	        },
	        Cancel: function () {
	            $(this).dialog("close");
	        }
	    }
	});

	$("#dialog-form").dialog({
	    resizable: false,
	    autoOpen: false,
	    height: 280,
	    width: 320,
	    modal: true,
	    buttons: {
	        "Add": function () {
	            var bValid = true;
	            allFields.removeClass("ui-state-error");
	            bValid = bValid && checkLength(value, "value", 3, 16);

	            if (bValid) {
	                $(".validateTips").html("");
//	                        $.getJSON('/Project/AddLookups?ProjectId=' + $("#ProjectId").val() + '&Type=' + type.val() + '&Value=' + value.val(),
//                            function (data) {
//                                if (data.Success == "False") {
//                                    value.addClass("ui-state-error");
//                                    updateTips(data.Message);
//                                    return;
//                                }
//                                else {
//                                    reload();
//                                    $("#dialog-form").dialog("close");
//                                }
	                //                            });
	                $.ajax({
	                    type: 'POST',
	                    url: '/Project/AddLookups?ProjectId=' + $("#ProjectId").val() + '&Type=' + type.val() + '&Value=' + value.val(),
	                    success: function (data) {
                            if (data.Success == "False") {
                                value.addClass("ui-state-error");
                                updateTips(data.Message);
                                return;
                            }
                            else {
                                reload();
                                $("#dialog-form").dialog("close");
                            }
	                    },
	                    dataType: 'json'
	                });
	            }
	        },
	        Cancel: function () {
	            $(this).dialog("close");
	        }
	    },
	    close: function () {
	        allFields.val("").removeClass("ui-state-error");
	    }
	});

	$("#dialog-message").dialog({
	    autoOpen: false,
	    modal: true,
	    buttons: {
	        Ok: function () {
	            $("#modal-msg").html("");
	            $(this).dialog("close");
	            $("#dialog-confirm").dialog("close");
	            reload();
	        }
	    }
	});

	$("#create-lookups")
	.button()
	.click(function () {
	    $("#dialog-form").dialog("open");
	});

	$("#tabs").tabs();
	$(".btnDelete").click(function () {
	    delType = $(this).attr("Type");
	    delValue = $(this).attr("Value");
	    row = $(this).parent().parent();
	    $("#dialog-confirm").dialog("open");
	});
});

$("#loading").ajaxStart(function () {
	$(this).center();
	$(this).show();
});

$("#loading").ajaxStop(function () {
	$(this).hide();
});